//
//  SearchViewController.swift
//  elarc01
//
//  Created by elarc on 16/2/26.
//  Copyright © 2016年 elarc. All rights reserved.
//

import UIKit
import LTMorphingLabel
import Gifu

/// test
class Coupon: NSObject {
    var btnId:String?
    var btnName:String?
}

class SearchViewController: UIViewController,LTMorphingLabelDelegate{
    // animation menu
    private lazy var presentationAnimator = GuillotineTransitionAnimation()
    //    label 动画
    private var i = -1
    private var textArray = [
        "What is design?",
        "Design", "Design is not just", "what it looks like", "and feels like.",
        "Design", "is how it works.", "- Steve Jobs",
        "Older people", "sit down and ask,", "'What is it?'",
        "but the boy asks,", "'What can I do with it?'.", "- Steve Jobs",
        "", "Swift", "Objective-C", "iPhone", "iPad", "Mac Mini",
        "MacBook Pro🔥", "Mac Pro⚡️",
        "爱老婆",
        "老婆和女儿"
    ]
    private var text: String {
        i = i >= textArray.count - 1 ? 0 : i + 1
        return textArray[i]
    }
    
    var label = LTMorphingLabel()
    
    //tabbar 转场
    lazy var gesture: UIPanGestureRecognizer = {
        let gesture = UIPanGestureRecognizer(target: self, action: #selector(SearchViewController.swipeTransition(_:)))
        return gesture
    }()
    
    func swipeTransition(sender: UIPanGestureRecognizer) {
        switch sender.state {
        case .Began :
            print("Began")
            if sender.translationInView(sender.view).x >= 0 {
                tabBarController?.tr_selected(0, gesture: sender)
            } else if sender.translationInView(sender.view).x < 0 {
                tabBarController?.tr_selected(2, gesture: sender)
            }
            
        default : break
        }
    }
    
    var button: HamburgerButton! = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addGestureRecognizer(gesture)
        self.view.backgroundColor = UIColor.whiteColor()
        // Do any additional setup after loading the view.
        
        let items = ["Scale","EVapor","Fall","Pixelate","Sparkle","Burn","Anvil"]
        let segmented=UISegmentedControl(items:items)
        segmented.frame = CGRectMake(0, 0, self.view.width - 40, 40)
        segmented.center=self.view.center
        segmented.selectedSegmentIndex=0
        segmented.setContentOffset(CGSizeMake(10,7),forSegmentAtIndex:1)
        segmented.tintColor=UIColor.redColor()
        segmented.addTarget(self, action: #selector(SearchViewController.segmentChanged(_:)), forControlEvents: UIControlEvents.ValueChanged)
        self.view.addSubview(segmented)
        
        
        // animation menu
        let navBar = self.navigationController!.navigationBar
        navBar.barTintColor = UIColor(red: 65.0 / 255.0, green: 62.0 / 255.0, blue: 79.0 / 255.0, alpha: 1)
        navBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
        let backBtn = UIButton(type: UIButtonType.Custom)
        backBtn.setImage(UIImage(named: "ic_menu"), forState: .Normal)
        
        backBtn.addTarget(self, action: #selector(showMenuAction), forControlEvents: .TouchUpInside)
        backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
        //            let btnW: CGFloat = ScreenWidth > 375.0 ? 50 : 44
        //            backBtn.frame = CGRectMake(0, 0, btnW, 40)
        backBtn.frame = CGRectMake(0, 0, 10, 18)
        
        self.navigationItem.leftBarButtonItem =  UIBarButtonItem(customView: backBtn);
        
        //humberButton
        buildHumberButton()
        
        //createImageGif
        createImageGif()
        
        //createActivity
        confirLoadActivity()
        
        //rotationButton
        buildRotaionButton()
    }
    
    var deformationBtn:RotationButton?
    func buildRotaionButton() -> Void {
        deformationBtn = RotationButton(frame: CGRectMake(100, 100, 94, 26), color: UIColor.whiteColor())
        self.view.addSubview(deformationBtn!)
        
        let cacheNumber = String().stringByAppendingFormat("%.2fM", FileCountManager.folderSize(LXCCachePath))
        deformationBtn!.forDisplayButton.setTitle(cacheNumber, forState: UIControlState.Normal)
        deformationBtn!.forDisplayButton.setTitle("最新排序", forState: UIControlState.Selected)
        deformationBtn!.forDisplayButton.titleLabel?.font = UIFont.systemFontOfSize(13);
        deformationBtn!.forDisplayButton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
        
        deformationBtn!.forDisplayButton.setImage(UIImage(named: "tabbar_home"), forState: UIControlState.Normal)
        
        deformationBtn!.addTarget(self, action: #selector(SearchViewController.btnEvent(_:)), forControlEvents: UIControlEvents.TouchDown)
    }

   
func btnEvent(sender:UIButton){
    sender.selected = !sender.selected

    FileCountManager.cleanFolder(LXCCachePath) { () -> () in
    }
    print("btnEvent", terminator: "")
}


    func confirLoadActivity(){
        var loadActivityView : DTIActivityIndicatorView?
        loadActivityView = DTIActivityIndicatorView(frame: CGRectMake(150, 500, 100, 100))
        self.view.addSubview(loadActivityView!)
        loadActivityView!.translatesAutoresizingMaskIntoConstraints = false
        loadActivityView!.indicatorColor = UIColor.purpleColor();
        loadActivityView!.indicatorStyle = "chasingDots";
        loadActivityView!.backgroundColor = UIColor.clearColor();
        loadActivityView?.startActivity()
    }
    
    func createImageGif() {
            let imageView = AnimatableImageView(frame: CGRect(x:133,y:600,width: 60,height: 60))
            self.view.addSubview(imageView)
            imageView.animateWithImage(named: "gif1.gif")
    }
    
    // animation menu
    func showMenuAction(sender: UIButton) {
        let menuVC = MenuAnimationViewController()
        menuVC.modalPresentationStyle = .Custom
        menuVC.transitioningDelegate = self
        if menuVC is GuillotineAnimationDelegate {
            presentationAnimator.animationDelegate = menuVC as? GuillotineAnimationDelegate
        }
        presentationAnimator.supportView = self.navigationController?.navigationBar
        presentationAnimator.presentButton = sender
        presentationAnimator.duration = 0.6
        self.presentViewController(menuVC, animated: true, completion: nil)
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        label.frame = CGRectMake(150, 200, 100,40)
        label.delegate = self
        label.text = text
        self.view.addSubview(label)
    }
    
    func buildHumberButton() -> Void {
        self.button = HamburgerButton(frame: CGRectMake(133, 400, 54, 54))
        button.backgroundColor = UIColor.redColor()
        self.button.addTarget(self, action: #selector(SearchViewController.toggle(_:)), forControlEvents:.TouchUpInside)
        
        self.view.addSubview(button)
    }
    
    func toggle(sender: AnyObject!) {
        self.button.showsMenu = !self.button.showsMenu
    }
    
    func changeText(sender: AnyObject) {
        label.text = text
        
    }
    
    func segmentChanged(sender: UISegmentedControl) {
        let seg = sender
        if let effect = LTMorphingEffect(rawValue: seg.selectedSegmentIndex) {
            label.morphingEffect = effect
            changeText(sender)
        }
    }
    
    func toggleLight(sender: UISegmentedControl) {
        let isNight = Bool(sender.selectedSegmentIndex == 0)
        view.backgroundColor = isNight ? UIColor.blackColor() : UIColor.whiteColor()
        label.textColor = isNight ? UIColor.whiteColor() : UIColor.blackColor()
    }
    
    func changeFontSize(sender: UISlider) {
        label.font = UIFont.init(name: label.font.fontName, size: CGFloat(sender.value))
        label.text = label.text
        label.setNeedsDisplay()
    }
    
    
    func morphingDidStart(label: LTMorphingLabel) {
        
    }
    
    func morphingDidComplete(label: LTMorphingLabel) {
        
    }
    
    func morphingOnProgress(label: LTMorphingLabel, progress: Float) {
        
    }
    
}

// animation menu
extension SearchViewController:UIViewControllerTransitioningDelegate {
    
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        presentationAnimator.mode = .Presentation
        return presentationAnimator
    }
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        presentationAnimator.mode = .Dismissal
        return presentationAnimator
    }
}
